package com.longrise.android.byjk.plugins.aboutme;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.Consum;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.adapter.ConsumAdapter;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.Tracker_Google;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by godliness on 2017/3/6.
 * 消费记录
 */

public class ConsumListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = "ConsumListActivity";

    private ConsumAdapter mConsumAdapter;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeLayout;

    private boolean mDestory;
    private Toolbar mToolbar;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {

        }
        return R.layout.activity_consumlist;
    }

    @Override
    public void initView() {
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout_consum);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyview_consum);
        setRecyclerViewLayoutManager();
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle("消费记录");
        setSupportActionBar(mToolbar);

        onEvent();
        adapter();

        if (checkNetWorkEnable()) {
            mSwipeLayout.setRefreshing(true);
            commit();
        } else {
            mSwipeLayout.setRefreshing(false);
        }

        Tracker_Google.getInstance().addGoogleScreenName("消费记录");
    }

    private void onEvent() {
        if (mSwipeLayout != null) {
            mSwipeLayout.setOnRefreshListener(this);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
    }

    private void adapter() {
        mConsumAdapter = new ConsumAdapter();
        mRecyclerView.setAdapter(mConsumAdapter);
    }

    private void commit() {
        EntityBean bean = new EntityBean();
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean serBean = new EntityBean();
        serBean.set("bean", bean);
        request("bbt_fee_orderList2", serBean);
    }

    private void request(String api, EntityBean bean) {
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, api, bean, mCallListener);
    }

    private LoadDataManager.OnRequestCallListener mCallListener = new LoadDataManager.OnRequestCallListener() {

        @Override
        public void onSuccess(String key, String service, Object result) {
            try {
                if (mDestory) {
                    return;
                }
                EntityBean bean = (EntityBean) result;
                int state = bean.getInt("resultstate");
                switch (state) {
                    case 0:
                        hintDialog(getResources().getString(R.string.hishint));
                        break;

                    case 1:
                        EntityBean[] set = (EntityBean[]) bean.get("result");
                        if (set == null) {
                            return;
                        }
                        parse(set);
                        break;

                    case 2:
                        hintDialog(getResources().getString(R.string.hishint));
                        break;

                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
            if (!mDestory) {
                showToast(getString(R.string.nonetwork));
            }
        }

        @Override
        public void onFinished(String key, String service) {
            if (mSwipeLayout != null && !mDestory) {
                mSwipeLayout.setRefreshing(false);
            }
        }
    };

    /**
     * 设置RecyclerView布局管理器
     */
    private void setRecyclerViewLayoutManager() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private void parse(EntityBean[] resultStr) {
//        List<Consum> consums = JsonUtil.fromJson(resultStr, new TypeToken<List<Consum>>() {
//        }.getType());
        List<Consum> consums = new ArrayList<>();
        for (EntityBean itemBea : resultStr) {
            Consum consum = new Consum();
            consum.total_fee = itemBea.getString("total_fee");
            consum.endtime = itemBea.getString("endtime");
            consum.out_trade_no = itemBea.getString("out_trade_no");
            consum.subject = itemBea.getString("subject");
            consum.trade_type = itemBea.getString("trade_type");
            consums.add(consum);
        }
        if (mConsumAdapter != null) {
            mConsumAdapter.clear();
            mConsumAdapter.addAll(consums);
        }
    }



    private void hintDialog(String msg) {
        View v = View.inflate(this, R.layout.dialog_zy_error, null);
        TextView text_Cancle = (TextView) v.findViewById(R.id.text_cancle_error_zy);
        TextView textMsg = (TextView) v.findViewById(R.id.text_error_zy);
        textMsg.setText(msg);
        final View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                if (v.getId() == R.id.text_cancle_error_zy) {
                    finish();
                }
            }
        };
        text_Cancle.setOnClickListener(onClickListener);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, v, 266, 171);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                    finish();
                }
                return false;
            }
        });
    }

    private void close() {
        finish();
    }

    @Override
    public void onRefresh() {
        if (checkNetWorkEnable()) {
            commit();
        } else {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DialogUtil.getInstance().dismiss();
        mDestory = true;
    }
}

